import os
from quant_researcher.quant.datasource_fetch.crypto_api.coingecko import CoinGeckoAPI
from quant_researcher.quant.project_tool.time_tool import date_shifter
import time
import pandas as pd


DATA_DIR = f'E:\\指标数据'  # 用于存放测试数据

file_path = os.path.join(DATA_DIR, r'trading_data')
cg = CoinGeckoAPI()
dex_exchange_id_list = \
    ['uniswap-bsc', 'uniswap_v2', 'uniswap_v3', 'uniswap_v3_arbitrum', 'uniswap-v3-avalanche', 'uniswap_v3_optimism', 'uniswap_v3_polygon_pos',
     'curve_arbitrum', 'curve_ethereum', 'curve_optimism',
     'apex_pro', 'maverick_protocol', 'ferro-protocol', 'openocean_finance', 'jupiter', 'orca', 'thorwallet', 'thorswap', 'woofi', 'pulsex', 'syncswap',
     'balancer-v1', 'balancer-v2', 'balancer_arbitrum',
     'sushiswap', 'sun_io',
     'pancakeswap_aptos', 'pancakeswap_ethereum', 'pancakeswap_new', 'pancakeswap_stableswap', 'pancakeswap-v3-bsc', 'pancakeswap-v3-ethereum',
     'dodo', 'dodo_arbitrum', 'dodo_bsc', 'dodo_polygon',
     'traderjoe', 'traderjoe-v2-1-arbitrum', 'traderjoe-v2-1-avalanche', 'traderjoe-v2-1-bsc', 'traderjoe-v2-arbitrum', 'traderjoe-v2-avalanche', 'traderjoe-v2-bsc',
     'quickswap', 'quickswap-polygon-zkevm', 'quickswap_v3',
     ]

# dex_list = list(set(dex_list))
# dex_list.sort()
# cg = CoinGeckoAPI()
# exchange_list = cg.get_exchanges_id_name_list()
# file_name = os.path.join(file_path, f'coingecko_exchange_list')
# exchange_list.to_excel(f'{file_name}.xlsx')
# dex_exchange_id_list = []
# for index, row in exchange_list.iterrows():
#     name = row['name']
#     id = row['id']
#     if name in dex_list:
#         dex_exchange_id_list.append(id)

dex_exchange_volume_list = []
for dex_exchange_id in dex_exchange_id_list:
    print(f'开始获取{dex_exchange_id}的交易额数据')
    try:
        exchange_volume_df = cg.get_exchanges_volume_chart_by_id(id=dex_exchange_id, days=30)
    except:
        print(f'获取{dex_exchange_id}的交易额数据失败')
    else:
        exchange_volume_df = exchange_volume_df[~exchange_volume_df.index.duplicated(keep='first')]
        exchange_volume_series = exchange_volume_df['amount']
        exchange_volume_series.name = dex_exchange_id
        dex_exchange_volume_list.append(exchange_volume_series)
        print(f'获取{dex_exchange_id}的交易额数据成功')
        time.sleep(8)

dex_exchange_volume_df = pd.concat(dex_exchange_volume_list, axis=1)
dex_exchange_volume_df.sort_index(inplace=True)
dex_exchange_volume_df['total'] = dex_exchange_volume_df.sum(axis=1)
file_name = os.path.join(file_path, f'dex_total_spot_amount_coingecko')
hist_dex_exchange_volume_df = pd.read_excel(f'{file_name}.xlsx', index_col='date')
previous_date = date_shifter(dex_exchange_volume_df.index[0], step='days', how_many=-1)
dex_exchange_volume_df = pd.concat \
    ([hist_dex_exchange_volume_df.loc[:previous_date, ], dex_exchange_volume_df.iloc[:-1, ]])  # 最新一天数据为当天，数据还不完整
# dex_exchange_volume_df.to_excel(f'{file_name}.xlsx')